﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;

namespace McMySkin_Installer
{
    public partial class InstallForm : NewForm
    {

        public InstallForm()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = "minecraft.jar";
            openFileDialog1.Title = "选择 minecraft.jar";
            openFileDialog1.Filter = "Minecraft Jar 文件(*.jar)|minecraft.jar";
            openFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            Config.installPath = openFileDialog1.FileName;
            label4.Text = "安装至:" + Config.installPath;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Config.installPath))
            {
                
                MessageBox.Show("文件没有选定 =w=");
            }
            else
            {

                if (checkBox1.Checked)
                {
                    Config.UseServerUrl = false;
                }
                else
                {
                    if (LoginStatus.Login)
                    {
                        Config.UseServerUrl = true;
                    }
                    else
                    {
                        MessageBox.Show("你必须先登陆到 McMySkin ！");
                    }
                }

                this.install();
            }
        }

        private void install()
        {
            if (File.Exists(Config.installPath))
            {
                if (Config.createBackup)
                {
                    File.Copy(Config.installPath, Config.installPath + ".mmsbak", true);
                }

                Installer installer = null;

                if (!string.IsNullOrEmpty(Config.customURL))
                {
                    installer = new Installer(Config.installPath, InstallTpye.INSTALL_MOJANG);
                }
                else
                {
                    installer = new Installer(Config.installPath, InstallTpye.INSTALL_MMS);
                }

                new Thread(() => { installer.Run(); }).Start();
                new Thread(() =>
                {   button1.Enabled = false;
                    while (!installer.done)
                    {
                        label2.Text = " " + progressBar1.Value + "%";
                        progressBar1.Value = (int)(installer.Progress * 100);
                    } MessageBox.Show("安装完成","完成",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    label2.Text = "未开始安装";
                    button1.Enabled = true;progressBar1.Value = 0;}).Start();
            
            }
            else
            {
                MessageBox.Show("文件不存在！","错误",MessageBoxButtons.OK,MessageBoxIcon.Stop);
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            pictureBox1.BackgroundImage = global::McMySkin_Installer.Properties.Resources.closeOnMouse;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.BackgroundImage = global::McMySkin_Installer.Properties.Resources.close;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
